package com.yzyz.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.yzyz.bean.news.NewsBean;

/**
 * 新闻缓存
 * @author David Sun
 * @Date   2010-4-23
 *
 */
public class NewsStore {
	private Map<Integer, NewsBean> cache = new HashMap<Integer, NewsBean>();
	
	/**
	 * 初始化cache
	 * @param list
	 */
	public synchronized void init(List<NewsBean> list){
		cache.clear();
		if(list != null){
			for(NewsBean news: list){
				cache.put(news.getId(), news);
			}
		}
	}
	
	/**
	 * 获得某个旅游资讯
	 * @param newsId
	 * @return
	 */
	public synchronized NewsBean get(int newsId){
		return cache.get(newsId);
	}
	
	public synchronized List<NewsBean> getAll(){
		List<NewsBean> list = new ArrayList<NewsBean>();
		Set<Entry<Integer, NewsBean>>  entrys = this.cache.entrySet();
		for(Entry<Integer, NewsBean> entry: entrys){
			list.add(entry.getValue());
		}
		return list;
	}
}
